package com.freak.neteasecloudmusic.modules.find.recommend.songlist.detail.comment.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.detail.comment.entity.CommentHeadEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.detail.comment.entity.CommentTypeEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.detail.comment.entity.CommentsEntity;
import com.freak.neteasecloudmusic.utils.TimeUtils;

import java.util.List;

/**
 * Created by Freak on 2020/3/7.
 */
public class CommentAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    private Context mContext;
    public final static int COMMENT_HEAD = 0;
    public final static int COMMENT_TYPE = 1;
    public final static int COMMENT_ITEM = 2;


    public CommentAdapter(List<MultiItemEntity> data, Context context) {
        super(data);
        mContext = context;
        addItemType(COMMENT_HEAD, R.layout.view_item_comment_head);
        addItemType(COMMENT_TYPE, R.layout.view_item_comment_type);
        addItemType(COMMENT_ITEM, R.layout.view_item_comment);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case COMMENT_HEAD:
                CommentHeadEntity commentHeadEntity = (CommentHeadEntity) item;
                Glide.with(mContext).load(commentHeadEntity.getUrl()).thumbnail(0.f).into((ImageView) helper.getView(R.id.imageViewCommentItemHeadBg));
                helper.setText(R.id.textViewCommentItemHeadContext, commentHeadEntity.getContext());
                helper.setText(R.id.textViewCommentItemHeadAuthor, commentHeadEntity.getAuthor());
                helper.addOnClickListener(R.id.textViewCommentItemHeadAuthor);
                break;
            case COMMENT_TYPE:
                CommentTypeEntity commentTypeEntity = (CommentTypeEntity) item;
                helper.setText(R.id.textViewCommentItemTypeName, commentTypeEntity.getTypeName());
                helper.setText(R.id.textViewCommentItemTypeCount, commentTypeEntity.getCount());
                break;
            case COMMENT_ITEM:
                CommentsEntity commentsEntity = (CommentsEntity) item;
                helper.setText(R.id.textViewCommentItemUserName, commentsEntity.getUser().getNickname());
                helper.setText(R.id.textViewCommentItemUserCount, commentsEntity.getLikedCount()==0?"":String.valueOf(commentsEntity.getLikedCount()));
                helper.setText(R.id.textViewCommentItemUserContext, commentsEntity.getContent());
                if (commentsEntity.getBeReplied() != null && commentsEntity.getBeReplied().size() > 0) {
                    helper.getView(R.id.textViewCommentItemUserReplyContext).setVisibility(View.VISIBLE);
                    helper.getView(R.id.viewLineComment).setVisibility(View.VISIBLE);
                    helper.getView(R.id.textViewCommentItemUserReplyCount).setVisibility(View.VISIBLE);
                    helper.getView(R.id.viewCommentItemUserReplyCount).setVisibility(View.VISIBLE);
                    helper.setText(R.id.textViewCommentItemUserReplyContext, commentsEntity.getBeReplied().get(0).getContent());
                    helper.setText(R.id.textViewCommentItemUserReplyCount, String.format(mContext.getResources().getString(R.string.reply),String.valueOf(commentsEntity.getBeReplied().size())));
                }else {
                    helper.getView(R.id.textViewCommentItemUserReplyContext).setVisibility(View.GONE);
                    helper.getView(R.id.viewLineComment).setVisibility(View.GONE);
                    helper.getView(R.id.textViewCommentItemUserReplyCount).setVisibility(View.GONE);
                    helper.getView(R.id.viewCommentItemUserReplyCount).setVisibility(View.GONE);
                }
                helper.setText(R.id.textViewCommentItemUserTime, TimeUtils.format(TimeUtils.DEFAULT_PATTERN, commentsEntity.getTime()));
                Glide.with(mContext).load(commentsEntity.getUser().getAvatarUrl()).thumbnail(0.f).into((ImageView) helper.getView(R.id.imageViewCommentItemPhoto));

                break;
        }
    }
}
